/*
*************************************************************************
*西瑞克斯研发管理系统
*版 本: 1.0   
*日 期：02/08/2012    
*作 者： 王浩 QQ：873987060   
*************************************************************************   
*/  
package com.application.yfthesis.dao;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.hibernate.Session;

import com.application.yfthesis.entity.YfThesis;
import com.application.yfthesis.struts.form.YfThesisForm;
import com.manager.commons.CommonUtil;
import com.manager.commons.DateUtil;
import com.manager.commons.dao.HibernateGenericDao;
import com.manager.commons.page.PageBean;
 /**
 * 
 * Description:YfThesisDAO<br />
 * 
 * Version:V0.1<br />
 * 
 * Author: 王浩 QQ：873987060 <br />
 * 
 * History: <br />
 * 
 * 02/08/2012 
 * 
 */
public class YfThesisDAO extends  HibernateGenericDao<YfThesis>
{
        private static final Logger logger = Logger.getLogger(YfThesisDAO.class);

	private YfThesis yfThesis;

	public YfThesis getYfThesis() {
		return this.yfThesis;
	}

	public void setYfThesis(YfThesis yfThesis) {
		this.yfThesis = yfThesis;
	}

	public Integer getAllCountYfThesis(YfThesisForm yfThesisForm){
		List args = new ArrayList();
		StringBuffer sql = new StringBuffer();
		sql.append(" SELECT COUNT(0) FROM yf_thesis t left join  yf_project p on t.project_no=p.project_no left join department b on p.project_org = b.id WHERE 1=1 ");
		sql = getStringBuffer(yfThesisForm, sql, args);
		return super.getCount(sql.toString(), args.toArray());
	}
        
	public void deleteYfThesiss(YfThesisForm yfThesisForm){    	
    		String hql = " delete from YfThesis where id in  ( '" + yfThesisForm.getIds() + "')";
		Session session = this.getSession();
		try
		{
			session.createQuery(hql).executeUpdate();
		}
		catch(RuntimeException re)
		{
		        logger.error("deleteYfThesiss failed");	
			throw re;
		}
		finally
		{
			if (session != null)
			{
				releaseSession(session);
			}
		}
       }

	public List searchYfThesis(final YfThesisForm yfThesisForm,final PageBean pageBean){  
		List args = new ArrayList();
		StringBuffer sql = new StringBuffer();
		sql.append(" SELECT t.*,p.*,b.name project_org_name FROM yf_thesis t left join  yf_project p on t.project_no=p.project_no left join department b on p.project_org = b.id WHERE 1=1 ");
		sql = getStringBuffer(yfThesisForm, sql, args);
		sql.append(" order by t.create_date desc");
		return getPageInfo(pageBean, sql.toString(), args);

	}

	/**
	 * 构造查询条件
	 * 
	 * @param marketperson
	 * @param buf
	 * @param args
	 * @return
	 */
	private StringBuffer getStringBuffer(YfThesisForm yfThesisForm,StringBuffer buf, List args) {
		/*
		 * 需要加入查询条件时封装
		 */
		 if(StringUtils.isNotBlank(yfThesisForm.getProjectNo())){
        	 buf.append(" and t.project_no = ? ");
        	 args.add(yfThesisForm.getProjectNo());
		}
         if(StringUtils.isNotBlank(yfThesisForm.getThesisName())){
			buf.append(" and t.thesis_name like ? ");
			args.add("%" + yfThesisForm.getThesisName().trim() + "%");
		}
        if(StringUtils.isNotBlank(yfThesisForm.getAuthor())){
			buf.append(" and t.author like ? ");
			args.add("%" + yfThesisForm.getAuthor().trim() + "%");
		}			                
		if(null != yfThesisForm.getDomain() && yfThesisForm.getDomain().longValue() != 0){
			buf.append(" and t.domain = ?");
			args.add(yfThesisForm.getDomain());
		}
		                
		if (StringUtils.isNotBlank(yfThesisForm.getProjectName())) {
			buf.append(" and p.project_name like ? ");
			args.add("%" + yfThesisForm.getProjectName().trim() + "%");
		}
		if (StringUtils.isNotBlank(yfThesisForm.getProjectOrg())) {
			buf.append(" and p.project_org = ? ");
			args.add(yfThesisForm.getProjectOrg());
		}
		if(StringUtils.isNotBlank(yfThesisForm.getPublicationTimeStr())){
			buf.append(" and t.publication_time = ? ");
			args.add(DateUtil.parseDate(yfThesisForm.getPublicationTimeStr(), "yyyy-MM-dd"));
		}
		if(StringUtils.isNotBlank(yfThesisForm.getId())){
			buf.append(" and t.id = ? " );
			args.add(yfThesisForm.getId());
		}
		if(yfThesisForm.getStatus()!=null && yfThesisForm.getStatus().longValue()!=0){
			buf.append(" and t.status = ? " );
			args.add(yfThesisForm.getStatus());
		}
		buf = CommonUtil.filterDataThesis(buf,args);
		
		
		return buf;
	}
	
	

	
	
}
	